package com.turnengine.client.global.announcement.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.global.announcement.bean.IAnnouncement;
import com.turnengine.client.global.announcement.command.DeleteAnnouncement;
import com.turnengine.client.global.announcement.command.GetAnnouncementCount;
import com.turnengine.client.global.announcement.command.GetAnnouncementList;
import com.turnengine.client.global.announcement.command.IDeleteAnnouncement;
import com.turnengine.client.global.announcement.command.IGetAnnouncementCount;
import com.turnengine.client.global.announcement.command.IGetAnnouncementList;
import com.turnengine.client.global.announcement.command.IPostAnnouncement;
import com.turnengine.client.global.announcement.command.PostAnnouncement;
import com.turnengine.client.global.game.enums.GameLevel;
import java.util.List;

/**
 * The Global Announcement Command Set.
 */
public class GlobalAnnouncementCommandSet implements IGlobalAnnouncementCommandSet {

	@Override
	public int postAnnouncement(long loginId, int gameLevelId, GameLevel gameLevel, String subject, String body) {
		IPostAnnouncement command = new PostAnnouncement(loginId, gameLevelId, gameLevel, subject, body);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int getAnnouncementCount(int gameId) {
		IGetAnnouncementCount command = new GetAnnouncementCount(gameId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IAnnouncement> getAnnouncementList(int gameLevelId, GameLevel gameLevel) {
		IGetAnnouncementList command = new GetAnnouncementList(gameLevelId, gameLevel);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IAnnouncement>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean deleteAnnouncement(long loginId, int id) {
		IDeleteAnnouncement command = new DeleteAnnouncement(loginId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
